In your final repo, there should be an R markdown file that organizes all computational steps for evaluating your proposed Facial Expression Recognition framework.
This file is currently a template for running evaluation experiments. You should update it according to your codes but following precisely the same structure.
if(!require("EBImage")){
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")
}
Loading required package: EBImage
if(!require("R.matlab")){
install.packages("R.matlab")
}
Loading required package: R.matlab
R.matlab v3.6.2 (2018-09-26) successfully loaded. See ?R.matlab for help.
Attaching package: ‘R.matlab’
The following objects are masked from ‘package:base’:
getOption, isOpen
if(!require("readxl")){
install.packages("readxl")
}
Loading required package: readxl
if(!require("dplyr")){
install.packages("dplyr")
}
Loading required package: dplyr
Attaching package: ‘dplyr’
The following object is masked from ‘package:EBImage’:
combine
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
if(!require("readxl")){
install.packages("readxl")
}
if(!require("ggplot2")){
install.packages("ggplot2")
}
Loading required package: ggplot2
RStudio Community is a great place to get help:
https://community.rstudio.com/c/tidyverse.
if(!require("caret")){
install.packages("caret")
}
Loading required package: caret
Loading required package: lattice
library(R.matlab)
library(readxl)
library(dplyr)
library(EBImage)
library(ggplot2)
library(caret)
set.seed(0)
setwd("~/Google Drive (yw3285@columbia.edu)/RA/FER/Project3-FacialEmotionRecognition/doc")
# here replace it with your own path or manually set it in RStudio to where this rmd file is located.
# use relative path for reproducibility
Training images are located in data/train_images folder. The file names are in the format of XX_XXX_XXXX; the first 2 digits represent emotion index, the following 3 digits represent identity(person) index, and the last four digits can be ignored. In current step, we extract paths to images and fiducial points, and corresponding emotion indices and identity indices.
#form paths of training images
image.path <- paste("../data/train_images",
list.files("../data/train_images"),
sep="/")
#form paths of fiducial points corresponding to training images
tmp <- gsub(image.path, pattern = 'jpg', replacement = 'mat')
point.path <- gsub(tmp, pattern = 'images', replacement = 'points')
#extract emotion index from paths
emotion_idx <- substr(image.path, 22, 23)
emotion_idx <- as.numeric(ifelse(as.numeric(emotion_idx < 10), substr(emotion_idx, 2, 2), emotion_idx))
identity <- as.numeric(substr(image.path, 25, 27))
emotion_table <- read_xls("../data/AU_annotation_all_subjects.xls")
-
/
info <- data.frame(stringsAsFactors = F, image.path = image.path, point.path = point.path, identity = identity, emotion_idx = emotion_idx) %>% inner_join(emotion_table, by = c('emotion_idx' = 'idx')) %>% mutate(Index = 1:length(image.path))
head(info)
In this chunk, we have a set of controls for the evaluation experiments.
run.cv=TRUE # run cross-validation on the training set
K <- 5 # number of CV folds
run.feature.train=TRUE # process features for training set
run.test=TRUE # run evaluation on an independent test set
run.feature.test=TRUE # process features for test set
Using cross-validation or independent test set evaluation, we compare the performance of models with different specifications. In this Starter Code, we tune parameter k (number of neighbours) for KNN.
k = c(5,10,50,100,150,200)
model_labels = paste("KNN with K =", k)
#train-test split
n <- nrow(info)
n_train <- round(n*(4/5), 0)
train_idx <- sample(info$Index, n_train, replace = F)
test_idx <- setdiff(info$Index,train_idx)
If you choose to use extract features from images, such as using Gabor filter, R memory will exhaust all images are read together. The solution is to repeat reading a smaller batch and process them.
EBImage::readImage(image.path)
Error: vector memory exhausted (limit reached?)
Fiducial points are stored in matlab format. In this step, we read them and store them in a list.
readMat.matrix <- function(path){
#print(path)
return(round(readMat(path)[[1]],0))
}
#load fiducial points
fiducial_pt_list <- lapply(point.path, readMat.matrix)
The follow plots show how pairwise distance between fiducial points can work as feature for facial emotion recognition.
The third column is the distributions of vertical distances between right mouth corner(50) and the midpoint of the upper lip(52). For example, the distance of an happy face tends to be shorter than that of a sad face.
feature.R should be the wrapper for all your feature engineering functions and options. The function feature( ) should have options that correspond to different scenarios for your project and produces an R object that contains features and responses that are required by all the models you are going to evaluate later.
feature.Rfiducial_pt_list <- lapply(point.path, readMat.matrix)
source("../lib/feature.R")
tm_feature_train <- NA
if(run.feature.train){
tm_feature_train <- system.time(dat_train <- feature(fiducial_pt_list, train_idx))
}
tm_feature_test <- NA
if(run.feature.train){
tm_feature_test <- system.time(dat_test <- feature(fiducial_pt_list, test_idx))
}
save(dat_train, file="../output/feature_train.RData")
save(dat_test, file="../output/feature_test.RData")
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.Rtest.ROutput: training model specification
In this Starter Code, we use KNN to do classification.
source("../lib/train_knn.R")
source("../lib/test_knn.R")
source("../lib/cross_validation_knn.R")
if(run.cv){
err_cv <- matrix(0, nrow = length(k), ncol = 2)
for(i in 1:length(k)){
cat("k=", k[i], "\n")
err_cv[i,] <- cv.function(dat_train, K, k[i])
save(err_cv, file="../output/err_cv.RData")
}
}
Visualize cross-validation results.
if(run.cv){
load("../output/err_cv.RData")
err_cv <- as.data.frame(err_cv)
colnames(err_cv) <- c("mean_error", "sd_error")
err_cv$k = as.factor(k)
err_cv %>%
ggplot(aes(x = k, y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
}
if(run.cv){
model_best <- k[which.min(err_cv[,1])]
}
par_best <- list(k = model_best)
#tm_train=NA
#tm_train <- system.time(fit_train <- train(dat_train, par_best))
#save(fit_train, file="../output/fit_train.RData")
tm_test=NA
if(run.test){
load(file="../output/fit_train.RData")
tm_test <- system.time(pred <- test(model_best, dat_test))
}
accu <- mean(dat_test$emotion_idx == pred)
cat("The accuracy of model:", model_labels[which.min(err_cv[,1])], "is", accu, ".\n")
The accuracy of model: KNN with K = 21 is 0.242 .
library(caret)
confusionMatrix(pred, dat_test$emotion_idx)
Confusion Matrix and Statistics
Reference
Prediction 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 18 21 22 23 24 25 26
1 9 0 4 2 2 2 1 1 1 5 1 1 0 0 2 4 3 2 1 0 1 2
2 1 17 0 0 0 0 0 0 7 0 0 0 0 0 0 1 0 0 0 1 0 1
3 1 0 11 0 0 2 0 0 1 4 0 0 1 0 0 3 1 0 0 0 0 1
5 1 0 1 8 0 0 1 0 0 3 3 1 5 0 1 0 0 0 0 0 2 0
6 0 0 0 0 7 0 1 0 0 0 0 0 0 1 1 0 1 2 0 0 0 0
7 1 0 0 1 0 7 1 0 2 1 2 2 1 0 0 1 0 0 0 0 0 0
8 4 1 1 0 2 0 3 2 1 1 0 0 0 7 1 0 1 1 3 1 5 0
9 0 1 0 0 1 0 1 15 1 0 0 0 0 0 0 0 0 0 0 1 1 0
10 0 2 0 0 0 0 1 2 5 0 1 0 0 0 0 1 0 0 1 1 0 0
11 1 0 3 7 0 1 0 1 0 2 1 1 3 0 0 0 0 0 2 0 1 1
12 0 1 1 3 0 2 0 0 0 1 4 1 2 1 0 1 0 0 1 1 1 1
13 0 2 1 4 0 3 0 0 1 0 5 2 3 0 0 0 1 0 0 0 2 3
14 1 0 3 3 0 0 0 0 0 3 3 3 2 1 2 0 1 0 0 1 2 4
15 0 0 0 1 3 0 4 0 0 0 0 1 0 9 5 0 2 1 4 2 4 1
16 0 0 1 0 4 0 0 2 0 0 0 1 0 4 3 0 2 2 0 3 5 1
18 1 0 0 0 1 2 2 0 0 0 0 1 0 0 3 5 0 1 0 1 0 0
21 0 0 0 0 2 0 2 2 0 0 0 1 0 1 0 1 3 1 1 3 0 0
22 0 0 2 0 0 0 3 1 0 0 0 0 0 0 3 1 3 5 2 1 0 2
23 0 0 0 1 2 0 5 0 0 0 1 0 0 1 1 2 1 2 1 2 1 1
24 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 2 0 1 2 1 0
25 0 0 0 0 0 1 0 0 2 0 0 1 0 1 0 0 0 0 1 1 0 0
26 0 1 1 0 0 0 1 0 2 2 0 1 2 0 1 0 1 0 0 2 1 1
Overall Statistics
Accuracy : 0.242
95% CI : (0.2051, 0.282)
No Information Rate : 0.06
P-Value [Acc > NIR] : < 2.2e-16
Kappa : 0.2059
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: 1 Class: 2 Class: 3 Class: 5 Class: 6 Class: 7 Class: 8 Class: 9
Sensitivity 0.4500 0.6800 0.3667 0.2667 0.2917 0.3333 0.11111 0.5769
Specificity 0.9271 0.9768 0.9702 0.9617 0.9874 0.9749 0.93446 0.9873
Pos Pred Value 0.2045 0.6071 0.4400 0.3077 0.5385 0.3684 0.08824 0.7143
Neg Pred Value 0.9759 0.9831 0.9600 0.9536 0.9651 0.9709 0.94850 0.9770
Prevalence 0.0400 0.0500 0.0600 0.0600 0.0480 0.0420 0.05400 0.0520
Detection Rate 0.0180 0.0340 0.0220 0.0160 0.0140 0.0140 0.00600 0.0300
Detection Prevalence 0.0880 0.0560 0.0500 0.0520 0.0260 0.0380 0.06800 0.0420
Balanced Accuracy 0.6885 0.8284 0.6684 0.6142 0.6395 0.6541 0.52279 0.7821
Class: 10 Class: 11 Class: 12 Class: 13 Class: 14 Class: 15 Class: 16
Sensitivity 0.2174 0.09091 0.1905 0.11765 0.10526 0.3462 0.1304
Specificity 0.9811 0.95397 0.9645 0.94824 0.94387 0.9409 0.9476
Pos Pred Value 0.3571 0.08333 0.1905 0.07407 0.06897 0.2432 0.1071
Neg Pred Value 0.9630 0.95798 0.9645 0.96829 0.96391 0.9633 0.9576
Prevalence 0.0460 0.04400 0.0420 0.03400 0.03800 0.0520 0.0460
Detection Rate 0.0100 0.00400 0.0080 0.00400 0.00400 0.0180 0.0060
Detection Prevalence 0.0280 0.04800 0.0420 0.05400 0.05800 0.0740 0.0560
Balanced Accuracy 0.5993 0.52244 0.5775 0.53294 0.52457 0.6435 0.5390
Class: 18 Class: 21 Class: 22 Class: 23 Class: 24 Class: 25 Class: 26
Sensitivity 0.2500 0.1364 0.2941 0.05556 0.08696 0.0000 0.05263
Specificity 0.9750 0.9707 0.9627 0.95851 0.98532 0.9852 0.96881
Pos Pred Value 0.2941 0.1765 0.2174 0.04762 0.22222 0.0000 0.06250
Neg Pred Value 0.9689 0.9607 0.9748 0.96451 0.95723 0.9452 0.96281
Prevalence 0.0400 0.0440 0.0340 0.03600 0.04600 0.0540 0.03800
Detection Rate 0.0100 0.0060 0.0100 0.00200 0.00400 0.0000 0.00200
Detection Prevalence 0.0340 0.0340 0.0460 0.04200 0.01800 0.0140 0.03200
Balanced Accuracy 0.6125 0.5535 0.6284 0.50703 0.53614 0.4926 0.51072
Note that the accuracy is not high but is better than that of ramdom guess(4.5%).
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
cat("Time for constructing training features=", tm_feature_train[1], "s \n")
Time for constructing training features= 1.753 s
cat("Time for constructing testing features=", tm_feature_test[1], "s \n")
Time for constructing testing features= 0.185 s
#cat("Time for training model=", tm_train[1], "s \n")
cat("Time for testing model=", tm_test[1], "s \n")
Time for testing model= 57.639 s